#include "BitOperation.h"

BitStream::BitStream(std::shared_ptr<BYTE> buffer, UINT32 capacity)
    :buffer(buffer)
    ,pointer(buffer)
    ,length(capacity * 8)
    ,capacity(capacity)
{
}

void BitStream::bitStreamPrefetch()
{
    if((pointer.get() - buffer.get()) < (capacity + 4))
        prefetch |= (*(pointer.get() + 4) << 24);
    if((pointer.get() - buffer.get()) < (capacity + 5))
        prefetch |= (*(pointer.get() + 5) << 16);
    if((pointer.get() - buffer.get()) < (capacity + 6))
        prefetch |= (*(pointer.get() + 6) << 8);
    if((pointer.get() - buffer.get()) < (capacity + 7))
        prefetch |= (*(pointer.get() + 7) << 0);
}

void BitStream::bitStreamFetch()
{
    accumulator = 0;
    if (pointer.get() - buffer.get() < capacity + 0)
        accumulator |= (*(pointer.get() + 0) << 24);
    if (pointer.get() - buffer.get() < capacity + 1)
        accumulator |= (*(pointer.get() + 1) << 16);
    if (pointer.get() - buffer.get() < capacity + 2)
        accumulator |= (*(pointer.get() + 2) << 8);
    if (pointer.get() - buffer.get() <capacity + 3)
        accumulator |= (*(pointer.get() + 3) << 0);
    bitStreamPrefetch();
}

//void BitStream::bitStreamFlush()
//{

//}

//void BitStream::bitStreamShift(int n)
//{

//}

//void BitStream::bitStreamWriteBits(int bits, int nbits)
//{

//}
